//
//  AppDelegate.m
//  lechuang
//
//  Created by Mr_hi on 2023/3/20.
//

#import "AppDelegate.h"
#import "LCTabBarViewController.h"
#import "LCNavigationViewController.h"
#import "LCLoginViewController.h"
#import <Bugly/Bugly.h>
#import "AppDelegate+InitThirdPlate.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    [self KeyboardManager];
    [self configSDWebImgSDK];
    
    [Bugly startWithAppId:@"0e41348dca"];
    
    [NSThread sleepForTimeInterval:1.5];
    
    [self initRootViewController];
    
    return YES;
}

- (void)initRootViewController {
    [self.window setBackgroundColor:[UIColor whiteColor]];
    if(LCUserDefGet(klogin_cre) == nil) {
        LCLoginViewController * loginVC = [LCLoginViewController xx_instantiateFromStoryboardNamed:@"Login"];
        loginVC.window = self.window;
        LCNavigationViewController * navtion = [[LCNavigationViewController alloc] initWithRootViewController:loginVC];
        self.window.rootViewController = navtion;
    } else {
        LCTabBarViewController *tabbar = [[LCTabBarViewController alloc] init];
        self.window.rootViewController = tabbar;
    }
    [self.window makeKeyAndVisible];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application {

}

#pragma mark - 回调
// iOS 9 之前用
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    [self webDoSomeThing:url];
    return YES;
}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
    [self webDoSomeThing:url];
    return YES;
}

// iOS 9之后
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options {
    [self webDoSomeThing:url];
    return YES;
}

#pragma mark - 协议通知
- (void)webDoSomeThing:(NSURL *)url {
    NSString *param = [[url query] stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSLog(@"url = %@ -- urlStr = %@",url,param);
    if ([param containsString:@"udid:"]) {
        param = [param stringByReplacingOccurrencesOfString:@"udid:"withString:@""];
        if (param.length == 40 || param.length == 25) {
            // 获取UDID并且去存储UDID
            LCUserDefSet(param, XWX_udid);
        }
    }
}

@end
